package com.sgb.kjwl.view.ui.activity.self.websiteManage;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bumptech.glide.Glide;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityWebsiteQualityUploadImg3Binding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.self.WebsiteQualityEntity;
import com.sgb.kjwl.model.entity.self.WebsiteTypeEntity;
import com.sgb.kjwl.utils.GlideEngine;
import com.swgk.core.dialog.RoundCornerDialog;
import com.sgb.kjwl.viewmodel.self.website.WebsiteRequalityUploadModel3;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

public class WebsiteQualityUploadImgActivity3 extends AppActivity implements ItemInputTextView.OnWholeItemClickListener {

    private ActivityWebsiteQualityUploadImg3Binding binding;

    @Inject
    WebsiteRequalityUploadModel3 qualityUploadModel;

    private List<WebsiteTypeEntity> rangeList = new ArrayList<>();
    private List<WebsiteTypeEntity> typeList = new ArrayList<>();
    private int zizhiType;
    private int zizhiRange;
    private String fileUrl = "";
    private File file;
    private String companyId = "";
    private WebsiteQualityEntity qualityEntity;

    public static void start(Context context, WebsiteQualityEntity entity, String id){
        Intent intent = new Intent(context,WebsiteQualityUploadImgActivity3.class);
        intent.putExtra("companyId",id);
        intent.putExtra("quality",entity);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder()
                .appComponent(appComponent)
                .selfModule(new SelfModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_website_quality_upload_img3);
        binding.setClickListener(this);
        binding.include.ivBack.setOnClickListener(view -> finish());
        binding.include.titleView.setText("资质上传");

        binding.qualityType.setOnWholeItemClickListener(this);
        binding.qualityRange.setOnWholeItemClickListener(this);
    }

    @Override
    protected void init() {

        qualityEntity = (WebsiteQualityEntity) getIntent().getSerializableExtra("quality");
        companyId = getIntent().getStringExtra("companyId");
        if(null==qualityEntity){
            binding.include.titleView.setText("资质上传");
            binding.tvUpload.setVisibility(View.VISIBLE);
            binding.img.setVisibility(View.GONE);
            binding.delete.setVisibility(View.GONE);
        }else{
            binding.include.titleView.setText("资质编辑");
            binding.tvUpload.setVisibility(View.GONE);
            binding.img.setVisibility(View.VISIBLE);
            binding.delete.setVisibility(View.VISIBLE);

            qualityEntity.setCompanyNo(companyId);
            binding.setViewModel(qualityEntity);
            qualityUploadModel.detailEntity.set(qualityEntity);
            binding.qualityRange.setContent(qualityEntity.getPermission()==1 ? "所有" : "会员");

            Glide.with(WebsiteQualityUploadImgActivity3.this).load(qualityEntity.getImage()).into(binding.img);
        }

        WebsiteTypeEntity entity = new WebsiteTypeEntity();
        entity.setId(1+"");
        entity.setName("所有");
        WebsiteTypeEntity entity1 = new WebsiteTypeEntity();
        entity1.setId(2+"");
        entity1.setName("会员");
        rangeList.add(entity1);
        rangeList.add(entity);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_upload:
                PictureSelector.create(WebsiteQualityUploadImgActivity3.this)
                        .openGallery(PictureMimeType.ofImage())
                        .loadImageEngine(GlideEngine.createGlideEngine())
                        .theme(R.style.picture_default_style)
                        .selectionMode(PictureConfig.SINGLE)
                        .forResult(PictureConfig.CHOOSE_REQUEST);
                break;
            case R.id.save:
                checkData();
                break;
            case R.id.delete:
                deleteDialog();
                break;
        }
    }

    private void checkData(){
        if(TextUtils.isEmpty(qualityUploadModel.detailEntity.get().getImage()) && null==file){
            MToast.showToast(WebsiteQualityUploadImgActivity3.this, "请上传资质图片");
            return;
        }else if(TextUtils.isEmpty(binding.qualityName.getContent())){
            MToast.showToast(WebsiteQualityUploadImgActivity3.this, "请填写资质名称");
            return;
        }else if (TextUtils.isEmpty(binding.qualityType.getContent())) {
            MToast.showToast(WebsiteQualityUploadImgActivity3.this, "请选择资质类型");
            return;
        }else if (TextUtils.isEmpty(binding.qualityRange.getContent())) {
            MToast.showToast(WebsiteQualityUploadImgActivity3.this, "请选择展示权限");
            return;
        }

        if(null==file && !TextUtils.isEmpty(qualityUploadModel.detailEntity.get().getImage())){
            qualityUploadModel.saveQualityDetail(qualityUploadModel.detailEntity.get());
        }else{
            qualityUploadModel.uploadMutiFileModel(file);
        }
    }

    public void uploadImgSuccess(String urls){
        qualityUploadModel.detailEntity.get().setId(0+"");
        qualityUploadModel.detailEntity.get().setImage(urls);
        qualityUploadModel.detailEntity.get().setName(binding.qualityName.getContent());
        qualityUploadModel.detailEntity.get().setCompanyNo(companyId);
        qualityUploadModel.saveQualityDetail(qualityUploadModel.detailEntity.get());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == PictureConfig.CHOOSE_REQUEST) {// 图片结果回调
                // 图片、视频、音频选择结果回调
                List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                file = new File(selectList.get(0).getRealPath());
                MLog.d("图片路径：", selectList.get(0).getRealPath());
                if (file.exists()) {
                    MLog.d("图片", "存在");
                }
                binding.tvUpload.setVisibility(View.GONE);
                binding.img.setVisibility(View.VISIBLE);
                Glide.with(WebsiteQualityUploadImgActivity3.this).load(selectList.get(0).getRealPath()).into(binding.img);
            }
        }
    }

    @Override
    public void OnWholeItemClick(View view) {
        switch (view.getId()) {
            case R.id.quality_range:
                if(null!=rangeList && rangeList.size()>0){
                    showRangePop();
                }else{
                    qualityUploadModel.getQualityRange();
                }
                break;
            case R.id.quality_type:
                if(null!=typeList && typeList.size()>0){
                    showTypePop();
                }else{
                    qualityUploadModel.getQualityType();
                }
                break;
        }
    }

    public void showRangeData(){
        showRangePop();
    }

    public void showTypeData(List<WebsiteTypeEntity> list){
        typeList = list;
        showTypePop();
    }

    public void showRangePop(){
        OptionsPickerView businessOption = new OptionsPickerBuilder(WebsiteQualityUploadImgActivity3.this, (options1, options2, options3, v) -> {
            String content = rangeList.get(options1).getName();
            binding.qualityRange.setContent(content);
            zizhiRange = options1;
            qualityUploadModel.detailEntity.get().setPermission(Integer.parseInt(rangeList.get(options1).getId()));

        }).build();
        businessOption.setPicker(rangeList);
        businessOption.setSelectOptions(zizhiRange);
        businessOption.show();
    }

    public void showTypePop(){
        OptionsPickerView businessOption = new OptionsPickerBuilder(WebsiteQualityUploadImgActivity3.this, (options1, options2, options3, v) -> {
            String content = typeList.get(options1).getName();
            binding.qualityType.setContent(content);
            zizhiRange = options1;
            qualityUploadModel.detailEntity.get().setType(typeList.get(options1).getName());
        }).build();
        businessOption.setPicker(typeList);
        businessOption.setSelectOptions(zizhiRange);
        businessOption.show();
    }

    //删除弹窗
    private void deleteDialog() {
        View view = View.inflate(this, R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(getActivity(), 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失

        TextView tv_message = (TextView) view.findViewById(R.id.tv_message);
        TextView tv_title = view.findViewById(R.id.tv_title);
        TextView tv_logout_confirm = (TextView) view.findViewById(R.id.tv_logout_confirm);
        tv_title.setVisibility(View.VISIBLE);
        TextView tv_logout_cancel = (TextView) view.findViewById(R.id.tv_logout_cancel);
        tv_title.setText("温馨提示");
        tv_message.setText("您确定删除当前资质?");

        //确定
        tv_logout_confirm.setOnClickListener(v -> {
            //删除
            qualityUploadModel.deleteQuality(qualityUploadModel.detailEntity.get().getId());
            roundCornerDialog.dismiss();
        });

        //取消
        tv_logout_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
            }
        });
    }
}
